package org.jcms.web.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jcms.model.Manager;
import org.jcms.model.Order;
import org.jcms.model.Proof;
import org.jcms.model.Proofstate;
import org.jcms.model.State;
import org.jcms.service.ManagerServiceI;
import org.jcms.service.OrderBookServiceI;
import org.jcms.service.OrderServiceI;
import org.jcms.service.ProofstateServiceI;
import org.jcms.service.StateServiceI;
import org.jcms.uitils.RandomUUID;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

/**
 * 参数 ：orderId
 * 
 * @author Administrator
 * 
 */
public class ManagerOrderPayAndBuildProof extends HttpServlet {

	private static final long serialVersionUID = 1L;

	public ManagerOrderPayAndBuildProof() {
		super();
	}

	public void destroy() {
		super.destroy();
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		ApplicationContext ac = WebApplicationContextUtils
				.getRequiredWebApplicationContext(this.getServletContext());
		OrderServiceI orderServiceI = (OrderServiceI) ac
				.getBean("orderServiceImpl");
		StateServiceI stateServiceI = (StateServiceI) ac
				.getBean("stateServiceImpl");
		ManagerServiceI managerServiceI = (ManagerServiceI) ac
				.getBean("managerServiceImpl");
		ProofstateServiceI proofstateServiceI = (ProofstateServiceI) ac
				.getBean("proofstateServiceImpl");
		OrderBookServiceI orderBookServiceI = (OrderBookServiceI) ac
				.getBean("orderBookServiceImpl");
		Manager manager = (Manager) request.getSession().getAttribute(
				"manager-0");
		Manager manager2 = (Manager) request.getSession().getAttribute(
				"manager-1");
		String retJson = "{\"error\":@err,\"msg\":\"@msg\"}";
		String err = "1";
		String msg = "";
		String orderId = request.getParameter("orderId");
		try {
			if (manager == null && manager2 == null) {
				msg = "网页已过期";
				return;
			}
			if (manager == null) {
				msg = "只有教材发行人员才有该操作权限";
				return;
			}
			if (orderId == null || "".equals(orderId)) {
				msg = "订单ID不能为空";
				return;
			}
			Order order = orderServiceI.findById(orderId);
			if (order == null) {
				msg = "要付款的订单不存在";
				return;
			}

			if (order.getState().getStateId() != 1) {
				if (order.getState().getStateId() == 2) {

					msg = "该订单已经付过款，无需再次付款";
				} else {
					if (order.getState().getStateId() == 3)
						msg = "该订单已完结，不能付款";
					else
						msg = "该订单状态为:" + order.getState().getStateName()
								+ "，不能完成付款操作";
				}

				return;
			}
			State state = stateServiceI.findById(2);
			order.setState(state);

			Proofstate proofstate = proofstateServiceI.findById(0);
			Proof proof = new Proof(RandomUUID.getRandomUUID(), proofstate,
					order);

			managerServiceI.payAndBuildProof(order, proof, orderBookServiceI);
			err = "0";
			msg = "订单(" + orderId + ")付款成功，并生成领书单，领书单号:" + proof.getProofId();

		} catch (Exception e) {
			msg = "系统出错";
		} finally {
			retJson = retJson.replace("@err", err);
			retJson = retJson.replace("@msg", msg);
			response.getOutputStream().write(retJson.getBytes("utf-8"));
		}
	}

	public void init() throws ServletException {

	}

}
